package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.domain.DataMaterialBatchHistory;
import com.uwlaser.service.DataMaterialBatchHistoryService;
import com.uwlaser.service.dto.DataMaterialBatchHistoryDto;
import com.uwlaser.service.dto.DataMaterialBatchHistoryQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.List;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "物料批次-历史管理")
@RequestMapping("/api/dataMaterialBatchHistory")
public class DataMaterialBatchHistoryController {

    private final DataMaterialBatchHistoryService dataMaterialBatchHistoryService;

    @GetMapping
    @Log("查询物料批次-历史")
    @ApiOperation("查询物料批次-历史")
    @PreAuthorize("@el.check('dataMaterialBatchHistory:list','dataMain:list')")
    public ResponseEntity query(DataMaterialBatchHistoryQueryParam query, Pageable pageable){
        return new ResponseEntity<>(dataMaterialBatchHistoryService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增物料批次-历史")
    @ApiOperation("新增物料批次-历史")
    @PreAuthorize("@el.check('dataMaterialBatchHistory:add')")
    public ResponseEntity create(@Validated @RequestBody DataMaterialBatchHistoryDto resources){
        return new ResponseEntity<>(dataMaterialBatchHistoryService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改物料批次-历史")
    @ApiOperation("修改物料批次-历史")
    @PreAuthorize("@el.check('dataMaterialBatchHistory:edit')")
    public ResponseEntity update(@Validated @RequestBody DataMaterialBatchHistoryDto resources){
        dataMaterialBatchHistoryService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除物料批次-历史")
    @ApiOperation("删除物料批次-历史")
    @PreAuthorize("@el.check('dataMaterialBatchHistory:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        dataMaterialBatchHistoryService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/recover")
    @Log("恢复物料批次-历史")
    public ResponseEntity recover(@Validated @RequestBody List<DataMaterialBatchHistoryDto> resources){
        return new ResponseEntity<>(dataMaterialBatchHistoryService.recover(resources),HttpStatus.CREATED);
    }

    /*
    @Log("导出物料批次-历史")
    @ApiOperation("导出物料批次-历史")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dataMaterialBatchHistory:list')")
    public void download(HttpServletResponse response, DataMaterialBatchHistoryQueryParam query) throws IOException {
        dataMaterialBatchHistoryService.download(dataMaterialBatchHistoryService.queryAll(query), response);
    }*/

}
